package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Maps;
import com.ndp.fb.fbagent.service.CustomaudiencesFbServices;
import com.ndp.fb.model.fbagent.param.APIPriority;
import com.ndp.fb.model.fbagent.param.ApiCallBackTarget;
import com.ndp.fb.model.fbagent.param.FacebookParam;
import com.ndp.fb.model.fbagent.param.customaudience.CustomAudienceLookalikeParam;
import com.ndp.fb.model.fbagent.param.customaudience.CustomAudienceParam;
import com.ndp.fb.model.fbagent.param.customaudience.CustomAudienceUserParam;
import com.ndp.fb.model.fbagent.param.customaudience.LookalikeSpec;
import com.ndp.fb.model.fbagent.param.entrustaccount.EntrustAccountParam;
import com.ndp.fb.model.fbagent.result.common.FBSingleId;
import com.ndp.fb.util.StringUtil;
import com.restfb.Parameter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hunk.qin on 2015/3/10.
 */
@Service
public class CustomaudiencesFbServicesImpl extends AbstractFBagentService implements CustomaudiencesFbServices {

    @Override
    public void agreeeCustomAudienceItem(EntrustAccountParam param)
    {
        List<Parameter> params = adapter(param);
        String uri = "/act_" +param.getFbAccountId();
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void fetchCustomaudiencesForSimple(Long accountId, String token, String after, String fetchKey) {
        FacebookParam param=new FacebookParam();
        param.setFields("id,name,subtype,time_updated,approximate_count,delivery_status,operation_status");
        param.setFbAccountId(accountId);
        param.setAccessToken(token);
        //param.setResultClassName(FbCustomaudiencesSimple.class.getName());
        param.setCallBack(true);
        if(StringUtil.isNotEmpty(after))
            param.setAfter(after);
        param.setLimit(500);
        param.setApiCallbackTargetType(ApiCallBackTarget.CustomAudience.Customaudiences.getTargetType());
        param.setApiPriorityCode(APIPriority.FetchCustomaudiences.getCode());

        Map<String, String> equipment = new HashMap<>();
        equipment.put("accountId", String.valueOf(accountId));
        equipment.put("token", token);
        equipment.put("fetchKey", fetchKey);
        param.setEquipment(equipment);

        String uri = "/act_" +accountId + "/customaudiences";
        List<Parameter> params = adapter(param);
        makeGetRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void createCustomAudience(CustomAudienceParam param)
    {
        List<Parameter> params = adapter(param);
        String uri = "/act_" +param.getFbAccountId() + "/customaudiences";
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void fillToCustomAudienceId(CustomAudienceUserParam param)
    {
        String uri = "/" +param.getId() + "/users";
        List<Parameter> params = adapter(param);
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void updateCustomAudience(CustomAudienceParam param)
    {
        List<Parameter> params = adapter(param);
        String uri = "/" + param.getId();
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void deleteCustomAudience(CustomAudienceParam param)
    {
        param.setApiPriorityCode(APIPriority.DeleteOjbect.getCode());
        List<Parameter> params = adapter(param);
        String uri = "/" + param.getId();
        deleteObject(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void createCustomAudienceLookalike(CustomAudienceLookalikeParam param) {
        List<Parameter> params = adapter(param);
        String uri = "/act_" +param.getFbAccountId() + "/customaudiences";
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    public void createCustomAudienceLookalike(String AudienceLookalikeName, String country ,String ratio, Long fbAccountId, Long customAudienceId, Long customAudienceDbId, String token) {
        CustomAudienceLookalikeParam customAudienceLookalikeParam = new CustomAudienceLookalikeParam();
        customAudienceLookalikeParam.setName(AudienceLookalikeName);
        customAudienceLookalikeParam.setFbAccountId(fbAccountId);
        customAudienceLookalikeParam.setOriginAudienceId(customAudienceId);
        customAudienceLookalikeParam.setAccessToken(token);
        customAudienceLookalikeParam.setSubtype("lookalike");
        LookalikeSpec lookalikeSpec = new LookalikeSpec();
        lookalikeSpec.setType("custom_ratio");
        lookalikeSpec.setCountry(country);
        Double ratioDouble = Double.valueOf(ratio) / 100.0;
        lookalikeSpec.setRatio(ratioDouble);
        customAudienceLookalikeParam.setLookalikeSpec(lookalikeSpec);

        customAudienceLookalikeParam.setApiCallbackTargetType(ApiCallBackTarget.CustomAudience.AddCustomaudienceLookALike.getTargetType());
        customAudienceLookalikeParam.setApiPriorityCode(APIPriority.FetchCustomaudiences.getCode());
        customAudienceLookalikeParam.setCallBack(true);
        HashMap<String, String> equipment = Maps.newHashMap();
        equipment.put("customAudienceDbId", customAudienceDbId + "");
        customAudienceLookalikeParam.setEquipment(equipment);
        customAudienceLookalikeParam.setResultClass(FBSingleId.class);
        createCustomAudienceLookalike(customAudienceLookalikeParam);
    }

    public void createCustomAudienceLookalikeByPage(Long pageId, String country, String ratio, String audienceLookalikeName, Long fbAccountId, String token, Long customAudienceDbId) {
        CustomAudienceLookalikeParam customAudienceLookalikeParam = new CustomAudienceLookalikeParam();
        customAudienceLookalikeParam.setName(audienceLookalikeName);
        customAudienceLookalikeParam.setFbAccountId(fbAccountId);

        customAudienceLookalikeParam.setSubtype("lookalike");
        LookalikeSpec lookalikeSpec = new LookalikeSpec();
        Double ratioDouble = Double.valueOf(ratio) / 100.0;
        lookalikeSpec.setRatio(ratioDouble);
        lookalikeSpec.setCountry(country);
        lookalikeSpec.setPageId(pageId);
        lookalikeSpec.setConversionType("page_like");
        customAudienceLookalikeParam.setLookalikeSpec(lookalikeSpec);

        customAudienceLookalikeParam.setApiCallbackTargetType(ApiCallBackTarget.CustomAudience.AddCustomaudienceLookALike.getTargetType());
        customAudienceLookalikeParam.setApiPriorityCode(APIPriority.FetchCustomaudiences.getCode());
        HashMap<String, String> equipment = Maps.newHashMap();
        equipment.put("customAudienceDbId", customAudienceDbId + "");
        customAudienceLookalikeParam.setCallBack(true);
        customAudienceLookalikeParam.setResultClass(FBSingleId.class);
        customAudienceLookalikeParam.setEquipment(equipment);
        customAudienceLookalikeParam.setAccessToken(token);
        createCustomAudienceLookalike(customAudienceLookalikeParam);
    }



    @Override
    public void updateCustomAudienceUsers(CustomAudienceUserParam param) {
        String uri = "/" +param.getId() + "/users";
        List<Parameter> params = adapter(param);

        if("delete".equalsIgnoreCase(param.getMethod())){
            deleteObject(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
        } else {
            makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
        }
    }

}